2020-2021 Sunseeker Telemetry and Lighting System
adc_ex2_intRef.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
67 //******************************************************************************
68 #include "driverlib.h"
69 #include "Board.h"
70 #define TIMER_PERIOD 80
71 
72 void main (void)
73 {
74  //Stop Watchdog Timer
75  WDT_A_hold(WDT_A_BASE);
76 
77  //Set A7 as an input pin.
78  //Set appropriate module function
79  GPIO_setAsPeripheralModuleFunctionInputPin(
80  GPIO_PORT_ADC7,
81  GPIO_PIN_ADC7,
82  GPIO_FUNCTION_ADC7);
83 
84  //Set LED1 as an output pin.
85  GPIO_setAsOutputPin(
86  GPIO_PORT_LED1,
87  GPIO_PIN_LED1);
88 
89  //Set LED2 as an output pin.
90  GPIO_setAsOutputPin(
91  GPIO_PORT_LED2,
92  GPIO_PIN_LED2);
93 
94  /*
95  * Disable the GPIO power-on default high-impedance mode to activate
96  * previously configured port settings
97  */
98  PMM_unlockLPM5();
99 
100  //Initialize the ADC Module
101  /*
102  * Base Address for the ADC Module
103  * Use internal ADC bit as sample/hold signal to start conversion
104  * USE MODOSC 5MHZ Digital Oscillator as clock source
105  * Use default clock divider of 1
106  */
107  ADC_init(ADC_BASE,
108  ADC_SAMPLEHOLDSOURCE_SC,
109  ADC_CLOCKSOURCE_ADCOSC,
110  ADC_CLOCKDIVIDER_1);
111 
112  ADC_enable(ADC_BASE);
113 
114  /*
115  * Base Address for the ADC Module
116  * Sample/hold for 16 clock cycles
117  * Do not enable Multiple Sampling
118  */
119  ADC_setupSamplingTimer(ADC_BASE,
120  ADC_CYCLEHOLD_16_CYCLES,
121  ADC_MULTIPLESAMPLESDISABLE);
122 
123  //Configure Memory Buffer
124  /*
125  * Base Address for the ADC Module
126  * Use input A7
127  * Use positive reference of Internally generated Vref
128  * Use negative reference of AVss
129  */
130  ADC_configureMemory(ADC_BASE,
131  ADC_INPUT_A7,
132  ADC_VREFPOS_INT,
133  ADC_VREFNEG_AVSS);
134 
135  ADC_clearInterrupt(ADC_BASE,
136  ADC_COMPLETED_INTERRUPT);
137 
138  //Enable Memory Buffer interrupt
139  ADC_enableInterrupt(ADC_BASE,
140  ADC_COMPLETED_INTERRUPT);
141 
142  //Internal Reference ON
143  PMM_enableInternalReference();
144 
145  //Configure internal reference
146  //If ref voltage no ready, WAIT
147  while (PMM_REFGEN_NOTREADY == PMM_getVariableReferenceVoltageStatus());
148 
149 #ifdef __MSP430_HAS_TBx__
150  // Configure TB0 to provide delay for reference settling ~75us
151  Timer_B_initUpModeParam initUpModeParam = {0};
152  initUpModeParam.clockSource = TIMER_B_CLOCKSOURCE_SMCLK;
153  initUpModeParam.clockSourceDivider = TIMER_B_CLOCKSOURCE_DIVIDER_1;
154  initUpModeParam.timerPeriod = TIMER_PERIOD;
155  initUpModeParam.timerInterruptEnable_TBIE = TIMER_B_TBIE_INTERRUPT_DISABLE;
156  initUpModeParam.captureCompareInterruptEnable_CCR0_CCIE =
157  TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
158  initUpModeParam.timerClear = TIMER_B_DO_CLEAR;
159  initUpModeParam.startTimer = true;
160  Timer_B_initUpMode(TB0_BASE, &initUpModeParam);
161 #else
162  // Configure TA0 to provide delay for reference settling ~75us
163  Timer_A_initUpModeParam initUpModeParam = {0};
164  initUpModeParam.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
165  initUpModeParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
166  initUpModeParam.timerPeriod = TIMER_PERIOD;
167  initUpModeParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
168  initUpModeParam.captureCompareInterruptEnable_CCR0_CCIE =
169  TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
170  initUpModeParam.timerClear = TIMER_A_DO_CLEAR;
171  initUpModeParam.startTimer = true;
172  Timer_A_initUpMode(TIMER_A0_BASE, &initUpModeParam);
173 #endif // __MSP430_HAS_TBx__
174 
175  __bis_SR_register(CPUOFF + GIE); // LPM0, TA0_ISR will force exit
176 
177  for (;;)
178  {
179  //Delay between conversions
180  __delay_cycles(5000);
181 
182  //Enable and Start the conversion
183  //in Single-Channel, Single Conversion Mode
184  ADC_startConversion(ADC_BASE,
185  ADC_SINGLECHANNEL);
186 
187  //LPM0, ADC_ISR will force exit
188  __bis_SR_register(CPUOFF + GIE);
189  //For debug only
190  __no_operation();
191 
192 
193  }
194 }
195 
196 //ADC interrupt service routine
197 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
198 #pragma vector=ADC_VECTOR
199 __interrupt
200 #elif defined(__GNUC__)
201 __attribute__((interrupt(ADC_VECTOR)))
202 #endif
203 void ADC_ISR (void)
204 {
205  switch (__even_in_range(ADCIV,12)){
206  case 0: break; //No interrupt
207  case 2: break; //conversion result overflow
208  case 4: break; //conversion time overflow
209  case 6: break; //ADCHI
210  case 8: break; //ADCLO
211  case 10: break; //ADCIN
212  case 12: //ADCIFG0
213 
214  //Automatically clears ADCIFG0 by reading memory buffer
215  //ADCMEM = A0 > 0.5V?
216  if (ADC_getResults(ADC_BASE) < 0x155) {
217  //Turn LED1 off
219  GPIO_PORT_LED1,
220  GPIO_PIN_LED1
221  );
222 
223  //Turn LED2 off
225  GPIO_PORT_LED2,
226  GPIO_PIN_LED2
227  );
228  }
229  else {
230  //Turn LED1 on
232  GPIO_PORT_LED1,
233  GPIO_PIN_LED1
234  );
235 
236  //Turn LED2 on
238  GPIO_PORT_LED2,
239  GPIO_PIN_LED2
240  );
241  }
242 
243  //Clear CPUOFF bit from 0(SR)
244  //Breakpoint here and watch ADC_Result
246  break;
247  default: break;
248  }
249 }
250 
251 #ifdef __MSP430_HAS_TBx__
252 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
253 #pragma vector=TIMER0_B0_VECTOR
254 __interrupt
255 #elif defined(__GNUC__)
256 __attribute__((interrupt(TIMER0_B0_VECTOR)))
257 #endif
258 void TB0_ISR (void)
259 {
260  TB0CTL = 0;
261  LPM0_EXIT; // Exit LPM0 on return
262 }
263 #else
264 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
265 #pragma vector=TIMER0_A0_VECTOR
266 __interrupt
267 #elif defined(__GNUC__)
268 __attribute__((interrupt(TIMER0_A0_VECTOR)))
269 #endif
270 void TA0_ISR (void)
271 {
272  TA0CTL = 0;
273  LPM0_EXIT; // Exit LPM0 on return
274 }
275 #endif // __MSP430_HAS_TBx__
void main(void)
void ADC_ISR(void)
void TA0_ISR(void)
#define TIMER_PERIOD
Timer_A_initUpModeParam initUpModeParam
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)